
package com.borqs.music.ui.adapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.NinePatchDrawable;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.borqs.music.R;
import com.borqs.music.model.SingerInfo;
import com.borqs.music.util.AsyncImageLoader;
import com.borqs.music.util.AsyncImageLoader.ImageCallback;

public class OnlineSingerListAdapter extends ArrayAdapter<SingerInfo> {
    private static final int TYPE_COUNT = 2;

    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_STATUS = 1;

    public static final int STATUS_NORMAL = -1;
    public static final int STATUS_MORE = -2;
    public static final int STATUS_FAILED = -4;
    public static final int STATUS_LOADING = -8;

    private static final SingerInfo ITEM_MORE;
    private static final SingerInfo ITEM_FAIL;
    private static final SingerInfo ITEM_LOAD;
    private static final SingerInfo ITEM_NORMAL;
    static {
        ITEM_MORE = new SingerInfo();
        ITEM_MORE.id = STATUS_MORE;

        ITEM_FAIL = new SingerInfo();
        ITEM_FAIL.id = STATUS_FAILED;

        ITEM_LOAD = new SingerInfo();
        ITEM_LOAD.id = STATUS_LOADING;

        ITEM_NORMAL = new SingerInfo();
        ITEM_NORMAL.id = STATUS_NORMAL;
    }

    private static class ViewHolder {
        public ImageView icon;
        public TextView line1;
        public TextView line2;
    }

    private static class StatusViewHolder {
        public TextView text;
        public ProgressBar progress;
    }

    private static final int layoutId = R.layout.track_list_item;
    private static final int statusId = R.layout.list_load_foot;
    private String mUnknownArtist;
    private NinePatchDrawable mDefaultAlbumIcon;
    private int mIconSideLength;
    private LayoutInflater mIinflater;
    private AsyncImageLoader mAsyncImageLoader;
    private ListView listView;

    public OnlineSingerListAdapter(Context context, List<SingerInfo> items, ListView listView) {
        super(context, 0, items);
        this.listView = listView;
        this.mIinflater = LayoutInflater.from(context);

        Resources r = context.getResources();
        mDefaultAlbumIcon = (NinePatchDrawable)r.getDrawable(R.drawable.albumart_mp_unknown_list);
        mIconSideLength = r.getDimensionPixelSize(R.dimen.track_list_item_height);
        mAsyncImageLoader = new AsyncImageLoader(mIconSideLength, mIconSideLength);
        mUnknownArtist = context.getString(R.string.unknown_artist_name);

    }

    @Override
    public long getItemId(int position) {
        return getItem(position).id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);
        SingerInfo singer = getItem(position);
        if (viewType == TYPE_NORMAL) {
            View returnView = null;
            ViewHolder vh = null;
            if (null == convertView) {
                returnView = mIinflater.inflate(layoutId, parent, false);
                vh = new ViewHolder();
                vh.icon = (ImageView) returnView.findViewById(R.id.icon);
                if (mIconSideLength > 0 ) {
                    vh.icon.setMinimumHeight(mIconSideLength);
                    vh.icon.setMinimumWidth(mIconSideLength);
                }
                vh.icon.setBackgroundDrawable(mDefaultAlbumIcon);
                vh.line1 = (TextView) returnView.findViewById(R.id.line1);
                vh.line2 = (TextView) returnView.findViewById(R.id.line2);
                vh.line2.setVisibility(View.GONE);
                returnView.setTag(vh);
            } else {
                returnView = convertView;
                vh = (ViewHolder) returnView.getTag();
                vh.icon.setImageBitmap(null);
            }

            if (singer.name.equals(MediaStore.UNKNOWN_STRING)) {
                singer.name = mUnknownArtist;
            }
            vh.line1.setText(singer.name);
            String imageUrl = singer.imgUrl;
            if (null != imageUrl && imageUrl.trim().length() > 0) {
                vh.icon.setTag(imageUrl);
                Bitmap cachedImage = mAsyncImageLoader.loadBitmap(imageUrl,
                        new ImageCallback() {
                            public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
                                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                                if (imageViewByTag != null) {
                                    imageViewByTag.setImageBitmap(imageDrawable);
                                }
                            }
                        });
                if (cachedImage == null) {
                    //vh.icon.setBackgroundDrawable(mDefaultAlbumIcon);
                    vh.icon.setImageBitmap(null);
                } else {
                    vh.icon.setImageBitmap(cachedImage);
                }
            }
            return returnView;
        }
        if (viewType == TYPE_STATUS) {
            View returnView = null;
            StatusViewHolder vh;
            if (convertView == null) {
                returnView = mIinflater.inflate(statusId, parent, false);
                vh = new StatusViewHolder();
                vh.text = (TextView) returnView.findViewById(R.id.text_load_next_page);
                vh.progress = (ProgressBar) returnView.findViewById(R.id.list_foot_progress);
                returnView.setTag(vh);
            } else {
                returnView = convertView;
                vh = (StatusViewHolder) returnView.getTag();
            }

            long status = getItemId(position);
            vh.progress.setVisibility(status == STATUS_LOADING ? VISIBLE : GONE);

            if (status == STATUS_MORE) {
                vh.text.setText(R.string.text_load_next_page);
            } else if (status == STATUS_LOADING) {
                vh.text.setText(R.string.text_loading_song);
            } else if (status == STATUS_FAILED) {
                vh.text.setText(R.string.text_load_failed);
            } else {
                // should never reach here.
                vh.text.setText("Internal Error!");
            }

            return returnView;
        }

        throw new IllegalStateException("The ItemViewType is Illegal at " + position + ", type = "
                + viewType);
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItemId(position) > -1) {
            return TYPE_NORMAL;
        } else {
            return TYPE_STATUS;
        }
    }

    private SingerInfo mCurrentStatus = ITEM_NORMAL;

    public int getStatus() {
        return ITEM_NORMAL.id;
    }

    public void setStatus(int status) {
        if (status == mCurrentStatus.id) {
            return;
        }

        if (mCurrentStatus.id != TYPE_NORMAL) {
            // remove old item
            remove(mCurrentStatus);
        }

        mCurrentStatus = getStatusSingerInfo(status);

        if (mCurrentStatus != ITEM_NORMAL) {
            add(mCurrentStatus);
        }
    }

    private SingerInfo getStatusSingerInfo(int status) {
        switch (status) {
            case STATUS_LOADING:
                return ITEM_LOAD;
            case STATUS_FAILED:
                return ITEM_FAIL;
            case STATUS_MORE:
                return ITEM_MORE;
            case STATUS_NORMAL:
                return ITEM_NORMAL;
            default:
                throw new IllegalArgumentException("The Illegal status " + status);
        }
    }
}
